# -*- coding: utf-8 -*-
# Author:jiang
# 2021/9/2 17:12
import shelve
from selenium import webdriver
from time import sleep
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import yaml,json
class TestCookies():
    def setup(self):
        self.url="http://10.80.1.6:8888"
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(5)
    def teardown(self):
        self.driver.quit()
    def test_get_cookies(self):
        self.driver.get(self.url)
        username, password = yaml.safe_load(open("../yaml/username.yaml", encoding="utf-8"))[0]
        self.driver.find_element(By.ID,'username').send_keys(username)
        self.driver.find_element(By.ID,'password').send_keys(password)
        sleep(5)
        self.driver.find_element(By.CSS_SELECTOR,"#formsubmit").click()
        sleep(1)
        cookies = self.driver.get_cookies()
        db = shelve.open("../conf/cookies/cookies")
        db["cookies"] = cookies
        db.close()
    def test_load_cookies(self):
        self.driver.get(self.url)
        db=shelve.open("../conf/cookies/cookies")
        cookies=db["cookies"]
        for cookie in cookies:
            if "expiry" in cookie.keys():
                cookie.pop("expiry")
            self.driver.add_cookie(cookie)
        self.driver.get(self.url)
        db.close()
        sleep(4)

